home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / DESKLIBC / SOURCES.ZIP / DeskLib / !DLSources / Libraries / Template / c / LoadFile < prev    next >
Text File  |  1995-05-19  |  6KB  |  189 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for 
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #                                      
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    Template.LoadFile.c
  12.     Author:  Copyright © 1992 Jason Williams
  13.              Thanks to John Winters for supplying the code that I hacked
  14.              changed, hacked, rewrote, and then wrote again from scratch!
  15.     Version: 1.12 (02 Mar 1994)
  16.     Purpose: Loading, cacheing, and retrieval of window templates
  17.              Now correctly loads templates with outline fonts in them
  18. */
  19.  
  20.  
  21. #include "TempDefs.h"
  22.  
  23.  
  24. /* Not intended for user consumption */
  25. linklist_header template_list         = {NULL, NULL};
  26. font_array      *template_fontarray   = (font_array *) -1;
  27.  
  28. /* 
  29. Provide function veneers for the above globals - they are refered to by the
  30. Window_ModeChange() function.
  31. */
  32. #ifdef _DLL
  33. extern linklist_header  *Template__Ref_list( void)      { return &template_list;      }
  34. extern font_array      **Template__Ref_fontarray( void) { return &template_fontarray; }
  35. #endif
  36.  
  37.  
  38.  
  39. static void ReadHeader(char *filename)
  40. /* Find out how many templates, names, and sizes */
  41. {
  42.   FILE *filehandle;
  43.   int  buffer[10];  /* 40 byte file buffer */
  44.   int  i, numtemplates;
  45.   char *s;
  46.   template_record *temprec;
  47.  
  48.   filehandle = fopen(filename, "rb");
  49.   if (filehandle == NULL)
  50.     Error_ReportFatalInternal(ERR7, ERRMESS7);
  51.  
  52.   if (fread(buffer, HEADER_SIZE, 1, filehandle) != 1)
  53.   {
  54.     fclose(filehandle);
  55.     Error_ReportFatalInternal(ERR4, ERRMESS4);
  56.   }
  57.  
  58.   numtemplates = 0;
  59.   while(TRUE)
  60.   {
  61.     if (fread(&buffer[0], INDEX_SIZE, 1, filehandle) != 1)
  62.     {
  63.       fclose(filehandle);
  64.       Error_ReportFatalInternal(ERR5, ERRMESS5);
  65.     }
  66.  
  67.     if (buffer[0] == 0) /* template list terminator */
  68.       break;
  69.  
  70.     numtemplates++;
  71.     temprec = (template_record *) malloc(sizeof(template_record));
  72.  
  73.     i = 0;
  74.     s = (char *) &buffer[3];
  75.     buffer[6] = 0;  /* Ensure terminators after 12-bytes of name */
  76.     while (TRUE)
  77.     {      
  78.       if (s[i] < 32)
  79.       {
  80.         temprec->identifier[i] = '\0';
  81.         break;
  82.       }
  83.       temprec->identifier[i] = s[i];
  84.  
  85.       i++;
  86.     }
  87.  
  88.     temprec->dataoffset   = buffer[0];
  89.     temprec->templatesize = buffer[1];     /* size needed to load template */
  90.     temprec->windowdef    = NULL;
  91.     temprec->indirectdata = NULL;
  92.     LinkList_Init(&(temprec->header));
  93.     LinkList_AddToTail(&template_list, &(temprec->header));
  94.   }
  95.  
  96.   fclose(filehandle);
  97. }
  98.  
  99.  
  100.  
  101. extern void Template_LoadFile(char *leafname)
  102. {
  103.   template_record *tptr;
  104.   char            filename[60];
  105.   char            tempname[20];
  106.   template_block  tblock;
  107.   char            tempdata[5192];        /* temp. buffer for indirected data */
  108.  
  109.   strcpy(filename, resource_pathname);
  110.   strcat(filename, leafname);
  111.  
  112.   /* Remember the end of the template list - this will be NULL for the
  113.    * first call to Template_LoadFile, and non-NULL for subsequent calls.
  114.    */
  115.   tptr = (template_record *) template_list.previous;
  116.  
  117.   ReadHeader(filename);     /* Find out how many templates, names, and sizes */
  118.   Wimp_OpenTemplate(filename);
  119.  
  120.   /* If tptr is NULL, then this is the first time we have loaded any templates,
  121.    * so get the head of the list to find the first blank record supplied by
  122.    * ReadHeader().  If tptr is not NULL, then we are appending templates, so
  123.    * get the next record after tptr, as this will be the first blank record
  124.    * just added by ReadHeader().
  125.    */
  126.   if (tptr == NULL)
  127.     tptr = (template_record *) template_list.next;
  128.   else
  129.     tptr = (template_record *) tptr->header.next;
  130.   
  131.   while (tptr != NULL)
  132.   {
  133.     tptr->windowdef = (window_block *) malloc(tptr->templatesize);
  134.     if (tptr->windowdef == NULL)
  135.     {
  136.       Wimp_CloseTemplate();
  137.       Error_ReportFatalInternal(ERR3, ERRMESS3);
  138.     }
  139.  
  140. /*  Now, read template once to determine the indirected data size needed.
  141.  *  I tried many different methods to do this, but thrashing in all the icons
  142.  *  and counting their indirect text buffer sizes, and then thrashing in
  143.  *  all of the validation strings and counting their lengths was slow, nasty,
  144.  *  and didn't seem very reliable.
  145.  *  This way also produces a much smaller code lump to include in DeskLib
  146.  *  It works very nicely, so long as your indirected data doesn't expand
  147.  *  to more than 5kB (unlikely unless you are being very antisocial towards
  148.  *  Mr. Wimp)
  149.  */
  150.     tblock.buffer   = tptr->windowdef;
  151.     tblock.workfree = tempdata;
  152.     tblock.workend  = tblock.workfree + 5188;
  153.  
  154.     /* The following line has been changed. If the font array was not passed
  155.      * in here as well, the fonts were not handled at all...
  156.      */
  157.     tblock.font     = template_fontarray;   /* was  (font_array *) -1 */
  158.     strcpy(tempname, tptr->identifier);
  159.     tblock.name     = tempname;
  160.     tblock.index    = 0;
  161.     Wimp_LoadTemplate(&tblock);
  162.  
  163.     tptr->indirectsize = (tblock.workfree - tempdata) + 4;
  164.     tptr->indirectdata = (char *) malloc(tptr->indirectsize);
  165.     if (tptr->indirectdata == NULL)
  166.     {
  167.       Wimp_CloseTemplate();
  168.       Error_ReportFatalInternal(ERR3, ERRMESS3);
  169.     }
  170.  
  171. /*  Now, do a Template_Load to actually load in the template. Should be nice
  172.  *  and quick, as the previous load should have cached the data in a buffer
  173.  *  so more disc reading is unlikely. (Though don't quote me on that! ;-)
  174.  */
  175.     tblock.buffer   = tptr->windowdef;
  176.     tblock.workfree = tptr->indirectdata;
  177.     tblock.workend  = tblock.workfree + tptr->indirectsize;
  178.     tblock.font     = template_fontarray;
  179.     strcpy(tempname, tptr->identifier);
  180.     tblock.name     = tempname;
  181.     tblock.index    = 0;
  182.     Wimp_LoadTemplate(&tblock);
  183.  
  184.     tptr = (template_record *) tptr->header.next;
  185.   }
  186.  
  187.   Wimp_CloseTemplate();
  188. }
  189.